package com.rongc.client.freight.business.supply.view.fragment;

import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.view.View;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.ApiUrl;
import com.rongc.client.freight.R;
import com.rongc.client.freight.UniApplication;
import com.rongc.client.freight.base.BaseFragment;
import com.rongc.client.freight.base.model.BaseDic;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/8/9.
 */

public class UserFragment extends BaseFragment {

    @Bind(R.id.tv_name)
    TextView mTvName;
    @Bind(R.id.tv_count)
    TextView mTvCount;
    @Bind(R.id.iv_head)
    ImageView mIvHead;
    @Bind(R.id.rating)
    RatingBar mRatingBar;
    @Bind(R.id.iv_phone)
    ImageView mIvPhone;

    public void hidePhone(){
        mIvPhone.setVisibility(View.GONE);
    }

    public void setData(String src, String name, String xiadancount,
                        String jiedancount, String rating,String phone){
        mTvName.setText(StringUtils.getName(name,phone));
        switch (UniApplication.getInstance().getType()) {
            case BaseDic.IdentityType.OWNER_TYPE:
                mTvCount.setText(String.format(
                        getResources().getString(R.string.waybill_mine_tip_driver),jiedancount));

                break;
            case BaseDic.IdentityType.DRIVING_TYPE:
                mTvCount.setText(String.format(
                        getResources().getString(R.string.waybill_mine_tip_owner),xiadancount));

                break;
        }

        if(StringUtils.isEmpty(rating) || "null".equals(rating)) {
            mRatingBar.setRating(5);
        } else {
            mRatingBar.setVisibility(View.VISIBLE);
            mRatingBar.setRating(StringUtils.parseFloat(rating));
        }
        mIvPhone.setTag(phone);

        Glide.with(getActivity().getApplicationContext())
                .load(ApiUrl.getImgUrl(src))
                .asBitmap()
                .centerCrop()
                .placeholder(R.mipmap.universal_head_default)
                .into(new BitmapImageViewTarget(mIvHead) {
                    @Override
                    protected void setResource(Bitmap resource) {
                        RoundedBitmapDrawable roundedBitmapDrawable =
                                RoundedBitmapDrawableFactory.create(getResources(), resource);
                        roundedBitmapDrawable.setCircular(true);
//                                        roundedBitmapDrawable.setCornerRadius(20); //设置圆角半径（根据实际需求）
                        roundedBitmapDrawable.setAntiAlias(true); //设置反走样
                        getView().setImageDrawable(roundedBitmapDrawable);
                    }
                });
    }

    @Override
    public int getLayoutId() {
        return R.layout.include_user;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {

    }

    @Override
    public void initListener() {
        mIvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String number = (String)v.getTag();
                ActivityUtils.callTo(getActivity(),number);
            }
        });
    }
}
